﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
  public  class SYS_SiftBdSynDAL
    {
        private string DbName;
        public SYS_SiftBdSynDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_SiftBdSyn model)
        {
            serverhelper sqlserverhelper = new serverhelper("");
            string primaryKey = sqlserverhelper.CreateKeyID();
            string sql = string.Format(@"insert into SYS_SiftBdSyn(
		    SB_ID
            ,Ruleword
            ,BdSyn
            ,SB_CaseCause
            ,HYCode
            ,Flag
            ,WMR
            ,WMS
            ,ApplicationFlag
            ,CreatePerson
            ,CreateTime)
			values ('{0}','{1}','{2}','{3}','{4}',{5},{6},{7},'{8}','{9}','{10}')", 
            primaryKey, model.Ruleword,model.BdSyn,model.SB_CaseCause,model.HYCode,model.Flag,model.WMR,model.WMS,model.ApplicationFlag,model.CreatePerson,DateTime.Now.ToString());
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sbID)
        {
            string sql = string.Format(@" delete from SYS_SiftBdSyn where SB_ID='{0}' ", sbID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取同义词表数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetSiftBdSyn()
        {
            string sql = " SELECT SB_ID,Ruleword,BdSyn  FROM SYS_SiftBdSyn ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取同义词表数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetRuleWord(string gzc,string tyc)
        {
            string sql = string.Format(@" select  * from WL_BDSyn where BDSynA='{0}' and BDSynB='{1}' ", gzc,tyc);
            Database oBase = DatabaseFactory.CreateDatabase("LdtCommonManage1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取同义词表数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetRuleWordNew(string gzc, string tyc)
        {
            string sql = string.Format(@" select a.HYCode,a.Flag as Flag1,b.Flag as Flag2,a.WMA,b.WMB,a.BS_ApplicationFlag as BS_ApplicationFlag1,b.BS_ApplicationFlag as BS_ApplicationFlag2,a.BS_CaseCause as BS_CaseCause1,b.BS_CaseCause as BS_CaseCause2,a.BDSynA,b.BDSynB from(select *from [dbo].[WL_BDSyn] where BDSynA='{0}')a join (select *from [dbo].[WL_BDSyn] where BDSynB='{1}')b on a.BDSynB=b.BDSynA ", gzc, tyc);
            Database oBase = DatabaseFactory.CreateDatabase("LdtCommonManage1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取同义词表数据(规则词)
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetRuleWordByGZC(string gzc)
        {
            string sql = string.Format(@"  select top 1 BS_CaseCause,HYCode,BS_ApplicationFlag from WL_BDSyn where BDSynA='{0}' ", gzc);
            Database oBase = DatabaseFactory.CreateDatabase("LdtCommonManage1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }
    }
}
